/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


myApp.controller('viewMealController',
        //['$scope'],
        function($scope, $location, mealService, $rootScope, $cookies) {
    
    $( ".filter_options_accordian" ).accordion({
      collapsible: true
    });
    
    $rootScope.isLoginPage = true;
    $rootScope.showSearchBar = true;
    $rootScope.showWelcomeMessage = true;
    var getMealSuccess = function(respData) {
        //get cookies
        var orders;
        try{
            orders = JSON.parse($cookies.mealOrders);
        }
        catch(err){
            orders = [];
        }
                
        var filteredMeals = respData.data;
        if(orders.length > 0){
            filteredMeals = [];
            //user has selected items
            for(index in respData.data){
                var meal = respData.data[index],
                isMealSelected = false;
                for(cookieIndex in orders){
                    order = orders[cookieIndex];
                    if(order.mealId == meal.mealId){
                        isMealSelected = true;
                        break;
                    }
                }
                if(isMealSelected === false){
                    filteredMeals.push(meal);
                }
            }
        }
        
        $scope.meals = filteredMeals;
    }
    var getMealFailure = function(errordata) {
    }
    mealService.getMeals().then(getMealSuccess, getMealFailure);
    
});